// 上传至oss的封装文件
import { ElMessage } from 'element-plus'
import { largeUpload } from '../components/Api/Upload'
import { size } from 'lodash'
// let basePath  = 'www.yourapi.com' // 你的服务器接口域名
/**
 * @description: 文件附件上传
 * file： 文件raw对象
 * successCallback： 成功的回调函数
 * errCallBack： 错误的回调函数
 * progressCallback： 上传进度的回调函数
 * dir: 上传阿里云目标文件夹 eg:图片image，视频video等
 */
const CHUNK_SIZE = 5 * 1024 * 1024 // 每块5MB
const MAX_SIZE = 30 * 1024 * 1024 // 30MB阈值
const uploadBig = async (file: any, successCallback = new Function(), errCallBack = new Function(), progressCallback = new Function()) => {
    // 小文件直接上传
    if (file.size <= MAX_SIZE) {
        const fd:any = new FormData()
        fd.append('file', file)
        fd.append('chunk_index', 0)
        fd.append('total_chunks', 1)
        fd.append('file_name', file.name)
        var fig: any = {
            onUploadProgress: (progressEvent: any) => {
                var complete = ((progressEvent.loaded / progressEvent.total) * 100) | 0
                if (complete < 100) {
                    progressCallback({
                        percent: complete
                    })
                }
            }
        }
        const res: any = await largeUpload(fd, fig)
        if (res.status === 200&& Number(res.data.is_finish)===1) {
            const obj={
                inch: res.data.data.path,
                name: res.data.data.name,
                real_name: res.data.data.real_name,
                size:file.size
            }
            successCallback(obj)
        } else {
            errCallBack(123)
        }
        return
    }
    // 大文件分块上传
    const totalChunks:any = Math.ceil(file.size / CHUNK_SIZE)
    let uploadedChunks = 0
    const fileName = file.name
    // 上传所有分块
    for (let i:number = 0; i < totalChunks; i++) {
        const start = i * CHUNK_SIZE;
        const end = Math.min(start + CHUNK_SIZE, file.size);
        const chunk = file.slice(start, end);
        const fd:any = new FormData()
        fd.append('file', chunk)
        fd.append('chunk_index', i)
        fd.append('total_chunks', totalChunks)
        fd.append('file_name', fileName)
        try {
            const fig = {
                onUploadProgress: (progressEvent: any) => {
                    const chunkProgress = Math.floor((progressEvent.loaded / progressEvent.total) * 100)
                    const overallProgress = Math.floor(((uploadedChunks + chunkProgress / 100) / totalChunks) * 100)
                    progressCallback({ percent: overallProgress })
                }
            }
            // 拿到结果后，做其他操作
            const res: any = await largeUpload(fd,fig)
            uploadedChunks++
            if (res.status === 200 && Number(res.data.is_finish)===1) {
                const obj={
                    inch: res.data.data.path,
                    name: res.data.data.name,
                    real_name: res.data.data.real_name,
                    size:file.size
                }
                successCallback(obj)
            }
            
        } catch (error) {
            errCallBack('分块上传失败')
            return
        }
    }
}
export default uploadBig
